﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Swirebev.PhotoDownload.Common;

namespace Swirebev.PhotoDownload.Cloud
{
    public class AVOSHelper
    {
        private bool _isProxy;
        public bool IsProxy
        {
            set
            {
                this._isProxy = value;
            }
        }

        private string gAppId;
        private string gAppKey;
        private string gUrlPrefix;
        private WebProxy gWebProxy;

        public AVOSHelper(string strAppId, string strAppKey, string strUrlPrefix)
        {
            this.gAppId = strAppId;
            this.gAppKey = strAppKey;
            this.gUrlPrefix = strUrlPrefix;

            this.gWebProxy = new WebProxy();
            Uri uri = new Uri(Settings.Instance.ProxyScheme + @"://" + Settings.Instance.ProxyUrl + @":" + Settings.Instance.ProxyPort);
            this.gWebProxy.Address = uri;
            this.gWebProxy.Credentials = new NetworkCredential(Settings.Instance.ProxyUserName, Settings.Instance.ProxyPassword);
            this._isProxy = false;
        }


        public JObject QueryAVClasses(string strClassName, string strWhere, int nLimit, string strOrder)
        {
            JObject obj2 = null;
            string format = "{0}/classes/{1}?where={2}&limit={3}&&order={4}";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(format, new object[] { this.gUrlPrefix, strClassName, strWhere, nLimit, strOrder }));
            request.ContentType = "application/json";
            request.Method = "GET";
            request.KeepAlive = true;
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Headers["X-AVOSCloud-Application-Id"] = this.gAppId;
            request.Headers["X-AVOSCloud-Application-Key"] = this.gAppKey;
            if (this._isProxy)
            {
                request.Proxy = this.gWebProxy;
            }
            try
            {
                WebResponse response = request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                obj2 = (JObject)JsonConvert.DeserializeObject(reader.ReadToEnd());
                responseStream.Close();
                reader.Close();
                response.Close();
                return obj2;
            }
            catch (Exception)
            {
            }
            return obj2;
        }

        public bool DeleteAVClass(string strClassName, string strObjectId)
        {
            JObject obj2 = null;
            bool flag = false;
            string format = "{0}/classes/{1}/{2}";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(format, this.gUrlPrefix, strClassName, strObjectId));
            request.ContentType = "application/json";
            request.Method = "DELETE";
            request.KeepAlive = true;
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Headers["X-AVOSCloud-Application-Id"] = this.gAppId;
            request.Headers["X-AVOSCloud-Application-Key"] = this.gAppKey;
            if (this._isProxy)
            {
                request.Proxy = this.gWebProxy;
            }
            try
            {
                WebResponse response = request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                obj2 = (JObject)JsonConvert.DeserializeObject(reader.ReadToEnd());
                responseStream.Close();
                reader.Close();
                response.Close();
                if ((obj2 != null) && (obj2.Count == 0))
                {
                    flag = true;
                }
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        //public string DownloadFile(string uri, string strFileName);
        

    }
}
